package com.siao.data
{
	import flash.utils.Dictionary;
	
	/**
	 * 键值映射 HashMap
	 * @author SiaoLeon
	 */
	public class HashMap
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		/**
		 * 键值映射数量
		 * @return
		 */
		public function get size():uint
		{
			return this.keys.length;
		}
		
		/**
		 * 所有键的数组
		 * @return
		 */
		public function get keys():Array
		{
			var result:Array = new Array();
			
			for (var key:Object in this.map)
			{
				result.push(key);
			}
			
			return result;
		}
		
		/**
		 * 所有值的数组
		 * @return
		 */
		public function get values():Array
		{
			var result:Array = new Array();
			
			for each (var value:* in this.map)
			{
				result.push(value);
			}
			
			return result;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var map:Dictionary;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function HashMap()
		{
			this.map = new Dictionary();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 根据值获取匹配的首个键
		 * @param value
		 * @return
		 */
		public function getKey(value:*):Object
		{
			for (var key:Object in this.map)
			{
				if (this.map[key] == value)
				{
					return key;
				}
			}
			
			return null;
		}
		
		/**
		 * 根据键获取映射的值
		 * @param key
		 * @return
		 */
		public function getValue(key:Object):*
		{
			return this.map[key];
		}
		
		/**
		 * 设置一个键值映射
		 * @param key
		 * @param value
		 * @return 该键所映射的值是否被更新
		 */
		public function put(key:Object, value:*):Boolean
		{
			if (this.map[key] == value)
			{
				return false;
			}
			
			this.map[key] = value;
			
			return true;
		}
		
		/**
		 * 移除一个键值映射
		 * @param key
		 */
		public function remove(key:Object):void
		{
			delete this.map[key];
		}
		
		/**
		 * 清除所有键值映射
		 */
		public function clear():void
		{
			for (var key:Object in this.map)
			{
				delete this.map[key];
			}
		}
		
		/**
		 * 检测映射是否包含指定的键
		 * @param key
		 * @return
		 */
		public function containsKey(key:Object):Boolean
		{
			return (this.map[key] != undefined);;
		}
		
		/**
		 * 检测映射是否包含指定的值
		 * @param value
		 * @return
		 */
		public function containsValue(value:*):Boolean
		{
			for each (var param:* in this.map)
			{
				if (param == value)
				{
					return true;
				}
			}
			
			return false;
		}
		
		/**
		 * 复制映射对象
		 * @return
		 */
		public function clone():HashMap
		{
			var result:HashMap = new HashMap();
			
			for (var key:Object in this.map)
			{
				result.put(key, this.getValue(key));
			}
			
			return result;
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
